<template>
  <div class="main">
    <Category
      @setFormInfo="setFormInfo"
      :show="isShowAdd"
      :list1="list1"
      :list2="list2"
      :list3="list3"
    ></Category>
    <el-card v-show="!isShowAdd">
      <el-button
        type="primary"
        icon="el-icon-plus"
        :disabled="!formInfo.category3Id"
        @click="isShowAdd = !isShowAdd"
      >
        添加</el-button
      >
      <el-table style="width: 100%" border :data="list">
        <el-table-column type="index" label="序号" width="100px" align="center">
        </el-table-column>
        <el-table-column prop="attrName" label="属性名称" width="150px">
        </el-table-column>
        <el-table-column prop="attrValueList" label="属性列表" width="width">
          <template slot-scope="{ row }">
            <el-tag type="success" v-for="r in row.attrValueList" :key="r.id">{{
              r.valueName
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button
              type="warning"
              icon="el-icon-edit"
              @click="updateAttr(row.id)"
              size="mini"
              >修改</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete-solid"
              @click="delTrade(row.id)"
              size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card v-show="isShowAdd">
      <el-form class="demo-form-inline">
        <el-form-item>
          <el-input
            placeholder="请输入属性名"
            style="width: 235px"
            v-model="attrInfo.attrName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-plus"
            :disabled="attrInfo.attrName ? false : true"
            @click="setAttr"
            >添加属性值</el-button
          >
          <el-button type="" icon="el-icon-delete-solid" @click="clearAttr"
            >取消</el-button
          >
        </el-form-item>
        <el-table style="width: 100%" border :data="attrInfo.attrValueList">
          <el-table-column type="index" label="序号" width="100px">
          </el-table-column>
          <el-table-column prop="prop" label="属性名" width="width">
            <template slot-scope="{ row, $index }">
              <el-input
                placeholder="请输入属性名"
                v-model="row.valueName"
                size="mini"
                v-if="row.flag"
                @blur="canBlur(row)"
                @keyup.native.enter="canBlur(row)"
                :ref="$index"
              ></el-input>
              <span
                v-else
                @click="toEdit(row, $index)"
                style="display: block"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column prop="id" label="操作" width="200px">
            <template slot-scope="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="delTrade(row.id)"
                size="mini"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-form-item style="margin-top: 10px">
          <el-button
            type="primary"
            @click="saveAttrInfo"
            :disabled="attrInfo.attrValueList.length < 1"
            >保存</el-button
          >
          <el-button type="" @click="setAttrShow">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { cloneDeep } from "lodash";
import cate from "@/utils/category";
export default {
  name: "Attr",
  mixins: [cate],
  data() {
    return {
      list1: [],
      list2: [],
      list3: [],
      formInfo: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      list: [],
      isShowAdd: false,
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    async setFormInfo(category1Id, category2Id, category3Id) {
      this.formInfo = {
        category1Id,
        category2Id,
        category3Id,
      };
      if (category1Id && category2Id && category3Id) {
        let result = await this.$api.default.category.getCategoryList(
          this.formInfo
        );
        if (result.code == 200) {
          let data = result.data;

          data.map((item) => {
            item.attrValueList.map((it) => {
              it.flag = false;
            });
            return item;
          });
          this.list = result.data;
        }
      }
    },
    setAttr() {
      this.attrInfo.categoryId = this.formInfo.category3Id;
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.attrId,
        valueName: "",
        flag: true,
      });
      this.$nextTick(() => {
        console.log(this.attrInfo.attrValueList.length);
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    setAttrShow() {
      this.isShowAdd = false;
      this.attrInfo.attrName = "";
      this.attrInfo.attrValueList = [];
    },
    async saveAttrInfo() {
      let { category1Id, category2Id, category3Id } = this.formInfo;
      if (
        this.attrInfo.attrValueList.length ||
        (this.attrInfo.id && !this.attrInfo.attrValueList.length)
      ) {
        let result = await this.$api.default.category.saveAttr(this.attrInfo);
        if (result.code == 200) {
          this.setAttrShow();
          this.$message({
            message: "操作成功!",
            type: "success",
          });
          this.setFormInfo(category1Id, category2Id, category3Id);
        }
      } else {
        this.$message({
          message: "操作失败,新品牌属性不能为空!",
          type: "warning",
        });
        this.setFormInfo(category1Id, category2Id, category3Id);
      }
    },
    clearAttr() {
      this.attrInfo.attrName = "";
      this.attrInfo.attrValueList = [];
    },
    updateAttr(id) {
      this.isShowAdd = true;
      let info = this.list.find((item) => item.id == id);
      info.del = true;
      this.attrInfo = cloneDeep(info);
    },
    canBlur(row) {
      if (row.valueName.trim()) {
        let status = this.attrInfo.attrValueList.some((item) => {
          if (item !== row) {
            return item.valueName == row.valueName;
          }
        });
        if (status) {
          this.$message({
            message: "输入的信息重复!",
            type: "warning",
          });
          return;
        }
        row.flag = false;
      } else {
        this.$message({
          message: "请输入完整信息!",
          type: "warning",
        });
      }
    },
    toEdit(row, index) {
      row.flag = true;
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    async delTrade(id) {
      if (this.attrInfo.del ? true : false) {
        this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(
          (item) => item.id != id
        );
      } else {
        let result = await this.$api.default.category.delAttrById(id);
        if (result.code == 200) {
          this.$message({
            message: "删除成功!",
            type: "success",
          });
          let { category1Id, category2Id, category3Id } = this.formInfo;
          this.setFormInfo(category1Id, category2Id, category3Id);
        }
      }
    },
    hander3(id3) {
      this.formInfo.category3Id = id3;
      let { category1Id, category2Id, category3Id } = this.formInfo;
      this.setFormInfo(category1Id, category2Id, category3Id);
    },
  },
};
</script>

<style>
.main {
  padding: 20px;
}
.el-card {
  margin-top: 5px;
}
.el-table {
  margin-top: 5px;
}
</style>
